package com.kahriboo.log4mon.server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.kahriboo.log4mon.client.LogEventService;
import com.kahriboo.log4mon.shared.LogEventShared;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class LogEventServiceImpl extends RemoteServiceServlet implements LogEventService {

	public LogEventServiceImpl() {
		super();
		// ConnectionListener.getInstance().startListener();
	}

	public LogEventShared[] getLogEvents(final String criteria) throws IllegalArgumentException {
		final int nb = 100;
		final LogEventShared[] result = new LogEventShared[nb];
		for (int i = 0; i < nb; i++) {
			result[i] = getLogEvent(i);
		}
		return result;
	}

	public LogEventShared getLogEvent(final int i) {
		// LogEvent builder (because LogEvent is immuable)
		final LogEventShared result = new LogEventShared();

		// LEVEL info
		result.setLevel(1);

		// LOCATION infos (optional)
		result.setClassName("className");
		result.setMethodName("methodName");
		result.setFileName("fileName");
		result.setLineNumber(12);

		// LOGGERNAME info
		result.setLoggerName("loggerName");

		result.setMessage("message_" + i);

		// THREADNAME info
		result.setThreadName("threadName");

		// final StackTrace.Builder stackStackTrace.newBuilder();
		// for (final String line : throwable) {
		// stackResult.addLine(line);
		// }
		//
		// result.setThrowable(stackResult);
		result.setThrowableClass("throwableClass");

		// TIMESTAMP info
		result.setTimeStamp(System.currentTimeMillis());

		// build the resulting object
		return result;
	}
}
